<?php
namespace App\Lib\RpcClient;

use App\Lib\Error\ErrorConstant;
use Swoft\Bean\Annotation\Mapping\Bean;

/**
 * Rpc 结果集
 * Class RpcResult
 * @package App\Lib\RpcClient
 * @Bean()
 */
class RpcResult implements RpcResultInterface
{

    /**
     * 错误码
     * @var int
     */
    private $code = 0;

    /**
     * RPC提示信息
     * @var string
     */
    private $message = '';

    /**
     * RPC相应结果
     * @var array
     */
    private $result = [];

    /**
     * 响应结果
     * @var array
     */
    private $data = [];

    /**
     * 获取一个RPCResult
     * @param array $result
     * @return RpcResult
     */
    public function newResult (array $result) {
        $RPCResult = clone $this;
        $RPCResult->data = $result;
        $RPCResult->code = $result['error_code'] ?? ErrorConstant::SUCCESS_CODE;
        $RPCResult->message = $result['message'] ?? '';
        $RPCResult->result = $result['result'] ?? [];
        return $RPCResult;
    }

    /**
     * @return mixed
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * @param mixed $code
     */
    public function setCode($code): void
    {
        $this->code = $code;
    }

    /**
     * @return mixed
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * @param mixed $message
     */
    public function setMessage($message): void
    {
        $this->message = $message;
    }

    /**
     * @return mixed
     */
    public function getResult()
    {
        return $this->result;
    }

    /**
     * @param mixed $result
     */
    public function setResult($result): void
    {
        $this->result = $result;
    }

    /**
     * @return array
     */
    public function getData(): array
    {
        return $this->data;
    }

    /**
     * @param array $data
     */
    public function setData(array $data): void
    {
        $this->data = $data;
    }
}
